﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 药库入库单录入 : Form
    {
        public static int rkdh = 0;//入库单号
        public int r0_flag = 0;//入库单flag
        public bool is_new = true;//是否为新单的标志
        public string gysbm ="";//供应商编码
        public int xh = 0;//序号

        public 药库入库单录入(int dh)
        {
            rkdh = dh;
            InitializeComponent();
            fresh();
            
        }

        public void fresh()//刷新
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext1 = "select  rkdh as 入库单号,xh as 序号,(select  convert(datetime,rq-2)) as 制单日期,CR.MC AS 入库类型,gys.mc as '供应商/退药科室'," +
                                "lrry as 录入,shry as 审核,jzry as 记账,bz 备注, r0.flag as 状态,r0.cgry as 采购人员  FROM YK_RKD0 R0 LEFT JOIN YK_CRLX CR ON R0.RKLX = CR.BM LEFT JOIN YK_ZDGYS gys ON R0.gysbm = gys.BM   " +
                                "WHERE r0.rkdh= " + rkdh;
                SqlCommand com = new SqlCommand(cmdtext1, cn);
                SqlDataReader dr = com.ExecuteReader();
                textBox1.Text = rkdh.ToString();
                if (dr.Read())
                {

                    comboBox1.Text = dr["入库类型"].ToString();
                    textBox13.Text = dr["采购人员"].ToString();
                    dateTimePicker1.Text = dr["制单日期"].ToString();
                    textBox12.Text = dr["供应商/退药科室"].ToString();
                    textBox6.Text = dr["序号"].ToString();
                    textBox7.Text = dr["录入"].ToString();
                    textBox9.Text = dr["审核"].ToString();
                    textBox10.Text = dr["记账"].ToString();
                    textBox8.Text = dr["备注"].ToString();
                    r0_flag = (int)dr["状态"];
                    is_new = false;
                    this.dataGridView1.AllowUserToAddRows = false;

                }
                else
                {
                    this.dataGridView1.AllowUserToAddRows = true;
                    is_new = true;
                }
                dr.Close();
                string cmdtext2 = "SELECT R1.ypbm 编码,YP.MC AS 名称,YP.GG as 规格,R1.SL as  数量,R1.jj as 进价,R1.jjje as 进价金额," +
                                   "   R1.pj as 批价,R1.pjje as  批价金额,R1.lsj as  零价,R1.ljje as  零价金额,R1.ph as  批号,R1.fph as  发票号,R1.bz as  备注" +
                                   " FROM YK_RKD1 R1 " +
                                   "   LEFT JOIN ZD_YP YP ON R1.YPBM = YP.BM" +
                                    " WHERE R1.RKDH =" + rkdh +
                                    " ORDER BY R1.AUTORKID";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext2, cn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.MultiSelect = false;


            if (!is_new)
            {
                textBox2.Text = dataGridView1.Rows.Count.ToString();
                decimal textBox3_Text = 0;
                decimal textBox4_Text = 0;
                decimal textBox5_Text = 0;
                for (int index = 0; index < dataGridView1.Rows.Count; index++)
                {
                    textBox3_Text = (decimal)dataGridView1[5, index].Value + textBox3_Text;
                    //textBox4_Text = (decimal)dataGridView1[7, index].Value + textBox4_Text;
                    textBox5_Text = (decimal)dataGridView1[9, index].Value + textBox5_Text;
                }
                textBox3.Text = textBox3_Text.ToString();
                textBox4.Text = textBox4_Text.ToString();
                textBox5.Text = textBox5_Text.ToString();
            }
            selectRow();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void selectRow()
        {
            if (r0_flag == 19)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
               
            }
            else if (r0_flag == 17)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = true;
               
            }
            else if (r0_flag == 16)
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button5.Enabled = false;
                button6.Enabled = false;
                
            }
        }

        private void button2_Click(object sender, EventArgs e)//审核
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_RKD0 SET " +
                              "SHRY = '"+LoginForm.username+"', " +
                              "JZRY = '', " +
                              "FLAG = 17  " +
                              "WHERE RKDH = " +rkdh +
                              "   COMMIT TRANSACTION ";
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button5_Click(object sender, EventArgs e)//消审
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_RKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                              "WHERE RKDH = " + rkdh +
                              "   COMMIT TRANSACTION ";
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button6_Click(object sender, EventArgs e)//记账
        {
            int jzrq = 0;//记账日期
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext4 = "select  convert(int,(select Convert(DateTime,getdate())))+1";
                SqlCommand cmd = new SqlCommand(cmdtext4, cn1);
                jzrq = (int)cmd.ExecuteScalar();
            }
            SqlConnection cn2 = new SqlConnection(SQL.conStr);
            {
                cn2.Open();
                string cmdtext3 = "BEGIN TRANSACTION  " +
                                  "UPDATE YK_RKD0 SET " +
                                  "JZRY = '"+LoginForm.username+"', " +
                                  "FLAG = 19,  " +
                                  "jzrq =" +jzrq+
                                  "  WHERE RKDH = " + rkdh +
                                  "   COMMIT TRANSACTION ";
                //陈立辉 加的
                string cmd_insert = "INSERT INTO YP_CRKJZ SELECT YPBM,SL FROM YK_RKD1 WHERE RKDH=" + rkdh;
                SqlCommand cmd1 = new SqlCommand(cmd_insert, cn2);
                cmd1.ExecuteNonQuery();
                //
                SqlCommand cmd = new SqlCommand(cmdtext3, cn2);
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("记账成功");
            fresh();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Form f = new 入库单编辑(rkdh,r0_flag);
            f.ShowDialog();
            fresh();
            r0_flag = 16;
        }

        private void button1_Click(object sender, EventArgs e)//保存
        {
            r0_flag = 16;
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            int dpt_1 = 0;
            int rklx = 0;//入库类型
            string cmdtext6 = "select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2";
            string cmdtext7 = "select bm from yk_crlx where mc='"+comboBox1.Text.ToString()+"'";
            SqlConnection cn1 = new SqlConnection(SQL.conStr);//入库日期
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    dpt_1 = (int)read[0];
                }
                read.Close();
                cn1.Close();
            }

            SqlConnection cn2 = new SqlConnection(SQL.conStr);//入库类型
            {
                cn2.Open();
                SqlCommand com = new SqlCommand(cmdtext7, cn2);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    rklx = System.Convert.ToInt32( read[0]);
                }
                read.Close();
                cn2.Close();
            }
            
            SqlConnection cn5 = new SqlConnection(SQL.conStr);//供应商编码
            {
                cn5.Open();
                string cmdtext5 = "SELECT bm as 编码   from YK_ZDGYS where  mc= '" + textBox12.Text+"'";
                SqlCommand com = new SqlCommand(cmdtext5, cn5);
                SqlDataReader read5 = com.ExecuteReader();
                if (read5.Read())
                {
                    gysbm = read5["编码"].ToString();
                }
                read5.Close();
            }
            if (is_new)
            {
                string cmdtext3 = " INSERT INTO YK_RKD0(XH,YKBM,RKDH,RQ,SJ,RKLX,JLLX,GYSBM,LRRY,SHRY,JZRY,FLAG,BZ,JZRQ,CGRY,CKDH,SHDH)" +
                                  "  VALUES(" + xh + ", '401', " + textBox1.Text + ", " + dpt_1 + ", 60914, " + rklx + ", 1, '" + gysbm + "', '" + LoginForm.username + "', '', '', 16, '" + textBox8.Text + "', 0, '" + textBox13.Text.ToString()+ "', 0, '') ";
                SqlConnection cn3 = new SqlConnection(SQL.conStr);
                {
                    cn3.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn3);
                    com.ExecuteNonQuery();
                }
                
            }
            else 
            {
                string cmdtext3 = " update YK_RKD0 set RQ=" + dpt_1 + ", RKLX=" + rklx + "  ,GYSBM='" + gysbm + "' ,BZ='" + textBox8.Text + "'  ,CGRY='" + textBox13.Text + "'  where rkdh= "+rkdh;
                cmdtext3 = cmdtext3 + "update yk_rkd1 set gysbm='"+gysbm+"'  where  rkdh= "+rkdh;
                SqlConnection cn4 = new SqlConnection(SQL.conStr);
                {
                    cn4.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn4);
                    com.ExecuteNonQuery();
                }
            }
            is_new = false;
            MessageBox.Show("保存成功");
        }

        private void 药库入库单录入_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.MultiSelect = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView2.ReadOnly = true;

            try
            {
                
                string cmdtext1 = "select xh from  yk_rkd0  order by xh desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    xh = (int)com.ExecuteScalar()+1;
                }
                textBox6.Text = xh.ToString();
            }
            catch  { return; } 
        }

        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)//供应商press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from yk_zdgys where dh like '" + textBox12.Text.Trim() + "%' or bm like '" + textBox12.Text.Trim() + "%' or mc like '" + textBox12.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
                dataGridView2.Columns[0].Width = 60;
                dataGridView2.Columns[1].Width = 150;
            }

        }

        public void select_bm()
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称  from yk_zdgys where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    textBox12.Text = read["名称"].ToString();
                    gysbm = read["编码"].ToString();
                }
                read.Close();
            }
            dataGridView2.Visible = false;
        }

        private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            select_bm();
        }

        private void textBox12_MouseClick(object sender, MouseEventArgs e)
        {
            dataGridView2.Visible = false;
        }
    }
}